<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_usermanagement extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize()
	{
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();				
		return true;
		
	}
	public function create_module($data)
	{
	$query="insert into `modules` (module_name,sequence,created_date,updated_date) VALUES ('".$data['module_name']."','".$data['sequence']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	$result=$this->conn->dbCommand($query);	
		//$this->db->last_query();exit;
	if($result){
			return array('message'=>'Module successfully created');			
		}else{
			return array('message'=>'module Creation Failed');
		}	
	}
	public function create_submodule($data)
	{
	$query="insert into `modules` (module_name,sequence,parent_module_id,file_name,created_date,updated_date) VALUES ('".$data['module_name']."','".$data['sequence']."','".$data['module_id']."','".$data['file_name']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	$result=$this->conn->dbCommand($query);	
		//$this->db->last_query();exit;
	if($result){
			return array('message'=>'subModule successfully created');			
		}else{
			return array('message'=>'submodule Creation Failed');
		}	
	}
	public function edit_module($data)
	{
	$query="update `modules` set module_name='".$data['module_name']."',sequence='".$data['sequence']."',updated_date='".date("Y-m-d H:i:s")."' where module_id=".$data['module_id'];
		$result=$this->conn->dbCommand($query);		
		if($result){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function edit_submodule($data)
	{
		
			$query="update `modules` set module_name='".$data['module_name']."',sequence='".$data['sequence']."',file_name='".$data['file_name']."',updated_date='".date("Y-m-d H:i:s")."' where module_id=".$data['module_id'];
		$result=$this->conn->dbCommand($query);		
		if($result){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function get_module($data)
	{
		$query="SELECT module_name,sequence,module_id,created_date FROM `modules` WHERE module_id=".$data['module_id'];
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Submodule type data','result'=>$result);		
		}else{
			return array('message'=>'submodule by id is failed');
		}	
	
	}
	public function get_submodule($data)
	{
		//print_r($data);exit;
	$query="SELECT module_name, sequence,file_name,module_id,created_date FROM `modules` where module_id=".$data['module_id']." and parent_module_id > 0";
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Submodule type data','result'=>$result);			
		}else{
			return array('message'=>'Submodule by id is failed');
		}	
	
	}
	public function delete_module($data)
	{
		$query="update `modules` set status='".$data['status']."' where module_id='".$data['module_id']."'";
		$data=$this->conn->dbCommand($query);
		if($data)
			return array('message'=>"Module status changed Successfully");		
		else
			return array('message'=>'Module status changed failed');
	}
	public function delete_submodule($data)
	{
		$query="update `modules` set status='".$data['status']."' where module_id='".$data['module_id']."' and parent_module_id > 0";
		$data=$this->conn->dbCommand($query);
		if($data)
			return array('message'=>"Submodule Deleted Successfully");		
		else
			return array('message'=>'Submodule Deletion Fails');
	}
	
	public function all_modules($data){
		
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	if (isset($data['search_by']) && $data['search_by']!='')
	{	//print_r($data);exit;
		
		if($data['search_by'] == 'module_name')
		{
			$query = "select module_name,sequence,created_date,module_id,status from modules where module_name like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
			$query1 = "select count(*) as total from modules where module_name like '%".$data['search_by_value']."%'";
		} 
	}else
	{$query="select module_name,sequence,created_date,module_id,status from modules order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$result=$this->conn->dbMultiple($query);
	$query1 = "SELECT count(*) as total from modules";
	}	
		$result=$this->conn->dbMultiple($query);
		if($result){
			$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All ","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No modules');
		
	}
	public function all_submodules($data)
	{	
		$current_page=0;
	$results_per_page=10;
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
		$result=$this->conn->dbMultiple("select * from modules where parent_module_id > '0'  limit ".$results_per_page." offset ".$offset);
		if($result){
			$totalrows = $this->conn->dbSingle('SELECT count(*) as total from modules where parent_module_id > "0"');
		return array('message'=>"All ","info"=>$result,'total'=>$totalrows,'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Submodules');
	}
}
	
?>